from ..ScriptWidget import ScriptWidget
from enums.WidgetType import WidgetType
from PySide6.QtWidgets import QLabel, QPushButton
from ..ScriptInput import ScriptInput
from windows.SubScriptEditWindow import SubScriptEditWindow


class For(ScriptWidget):
    def __init__(self, initType=None):
        super(For, self).__init__(WidgetType.FOR)
        self.initType = initType

        self.subScriptEditWindow = None
        self.labTitle = QLabel('循环:', self)
        self.labTitle.setFixedSize(30, self.lineHeight)
        self.addWidget(self.labTitle)

        self.newLineSpaceWidth = self.labTitle.width() + self.intervalX

        self.inputCount = ScriptInput(self, '次数')
        self.inputCount.text = 0
        self.addWidget(self.inputCount)

        # 编辑脚本
        self.btnEdit = QPushButton(self)
        self.btnEdit.clicked.connect(self.edit)
        self.btnEdit.setText('编辑')
        self.addWidget(self.btnEdit)

        if initType in ['edit', 'create']:
            self.addOperate()

    def edit(self):
        if self.subScriptEditWindow is None:
            self.subScriptEditWindow = SubScriptEditWindow(self)

        self.subScriptEditWindow.show()

    @property
    def data(self):
        return {
            "type": self.type.value,
            "count": int(self.inputCount.text),
            'sub_script': self.subScriptEditWindow.data,
            'enable': self.switchEnable.isChecked()
        }

    @data.setter
    def data(self, data):
        self.setData(data)
        self.inputCount.text = data['count']
        self.subScriptEditWindow = SubScriptEditWindow(self, data['sub_script'])
